package com.shuda.business.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuda.business.user.entity.RoleEntity;
import com.shuda.business.user.service.RoleService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.result.Result;
import com.shuda.common.utils.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 角色管理控制器类
 */
@RestController
@RequestMapping("/api/system/role")
@Tag(name = "系统角色管理", description = "系统角色管理相关接口")
@RequiredArgsConstructor
@Validated
public class RoleController {

    private final RoleService roleService;

    /**
     * 分页查询角色列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询角色列表")
    public Result<PageResult<RoleEntity>> getRolePage(@Valid PageDTO pageDTO,
                                                       @RequestParam(required = false) String roleName,
                                                       @RequestParam(required = false) String roleCode,
                                                       @RequestParam(required = false) String status) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        if (roleName != null && !roleName.trim().isEmpty()) {
            queryWrapper.like("role_name", roleName);
        }
        if (roleCode != null && !roleCode.trim().isEmpty()) {
            queryWrapper.like("role_code", roleCode);
        }
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("create_time");
        
        PageResult<RoleEntity> pageResult = roleService.getRolePage(pageDTO, queryWrapper);
        return Result.success(pageResult);
    }

    /**
     * 获取角色详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取角色详情")
    public Result<RoleEntity> getRoleById(@PathVariable Long id) {
        RoleEntity role = roleService.getById(id);
        return Result.success(role);
    }

    /**
     * 创建角色
     */
    @PostMapping
    @Operation(summary = "创建角色")
    public Result<String> createRole(@Valid @RequestBody RoleEntity role) {
        roleService.createRole(role);
        return Result.success("角色创建成功");
    }

    /**
     * 更新角色
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新角色")
    public Result<String> updateRole(@PathVariable Long id, @Valid @RequestBody RoleEntity role) {
        role.setId(id);
        roleService.updateRole(role);
        return Result.success("角色更新成功");
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    public Result<String> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.success("角色删除成功");
    }

    /**
     * 批量删除角色
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除角色")
    public Result<String> deleteRoles(@RequestBody List<Long> ids) {
        roleService.deleteRoles(ids);
        return Result.success("角色批量删除成功");
    }

    /**
     * 更新角色状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新角色状态")
    public Result<String> updateRoleStatus(@PathVariable Long id, @RequestParam String status) {
        roleService.updateRoleStatus(id, status);
        return Result.success("角色状态更新成功");
    }

    /**
     * 分配角色权限
     */
    @PutMapping("/{id}/permissions")
    @Operation(summary = "分配角色权限")
    public Result<String> assignPermissions(@PathVariable Long id, @RequestBody List<Long> permissionIds) {
        roleService.assignPermissions(id, permissionIds);
        return Result.success("权限分配成功");
    }

    /**
     * 获取角色权限
     */
    @GetMapping("/{id}/permissions")
    @Operation(summary = "获取角色权限")
    public Result<List<Long>> getRolePermissions(@PathVariable Long id) {
        List<Long> permissionIds = roleService.getRolePermissions(id);
        return Result.success(permissionIds);
    }

    /**
     * 获取所有角色
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有角色")
    public Result<List<RoleEntity>> getAllRoles() {
        List<RoleEntity> roles = roleService.list();
        return Result.success(roles);
    }
}